package net.liuyimin.controller;

import net.liuyimin.service.StockService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class StockController {

    @Resource
    private StockService stockService;

    /**
     * 基于Redis实现分布式锁: 演示高并发下的超卖问题
     */
    @GetMapping("stock/deduct")
    public String deduct(){
        this.stockService.deduct();
        return "hello stock deduct！！";
    }

    /**
     * 基于Redis实现分布式锁: 基本实现分布式锁
     */
    @GetMapping("stock/deduct2")
    public String deduct2(){
        this.stockService.deduct2();
        return "hello stock deduct2！！";
    }

    /**
     * 基于Redis实现分布式锁: 优化重试操作步骤,将递归调用换成循环
     */
    @GetMapping("stock/deduct3")
    public String deduct3(){
        this.stockService.deduct3();
        return "hello stock deduct3！！";
    }

    /**
     * 基于Redis实现分布式锁: 防死锁
     */
    @GetMapping("stock/deduct4")
    public String deduct4(){
        this.stockService.deduct4();
        return "hello stock deduct4！！";
    }

    /**
     * 基于Redis实现分布式锁: 使用Lua脚本解决防误删除的原子性问题
     */
    @GetMapping("stock/deduct5")
    public String deduct5(){
        this.stockService.deduct5();
        return "hello stock deduct5！！";
    }

    /**
     * 基于Redis实现分布式锁: 实现可重入锁+自动续期
     */
    @GetMapping("stock/deduct6")
    public String deduct6(){
        this.stockService.deduct6();
        return "hello stock deduct6！！";
    }

    /**
     * 基于Redis实现分布式锁: 借助 Redisson 快速实现分布式锁
     */
    @GetMapping("stock/deduct7")
    public String deduct7(){
        this.stockService.deduct7();
        return "hello stock deduct7！！";
    }

}
